package org.elong.qadev.reflect;

import java.lang.reflect.Field;

/**
 * 
 * @ClassName: ReflectOperateAttribute
 * @Description: TODO(通过类路径或类本身操作类属性的值)
 * @author kangchen
 * @date 2015年10月15日
 *
 */
public class ReflectOperateAttribute {
	static Class<?> Clazz = null;
	static Object obj = null;
	static String attribValue;

	/**
	 * 反射获取已存在的属性值
	 * 
	 * @author M.F.Yang
	 * @param classPath
	 * @param attributeValue
	 * @return
	 */
	public static String getAttributeValue(String classPath,
			String attributeValue) {
		try {
			Clazz = Class.forName(classPath);
			obj = Clazz.newInstance();
			Field field = Clazz.getDeclaredField(attributeValue);
			field.setAccessible(true);
			attribValue = (String) field.get(attributeValue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return attribValue;
	}

	/**
	 * 
	 * @Title: getAttributeValueByClass
	 * @Description: TODO(通过类本身获取属性值)
	 * @param @param input
	 * @param @param attributeValue
	 * @param @return 参数
	 * @return String 返回类型
	 * @throws
	 */
	public static String getAttributeValueByClass(Class<?> input,
			String attributeValue) {
		try {
			obj = input.newInstance();
			Field field = input.getDeclaredField(attributeValue);
			field.setAccessible(true);
			attribValue = (String) field.get(attributeValue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return attribValue;
	}
	
	public static Object getAttributeObjectByClass(Class<?> input,
			String attributeValue) {
		Object o = null;
		try {
			obj = input.newInstance();
			Field field = input.getDeclaredField(attributeValue);
			field.setAccessible(true);
			o  = field.get(attributeValue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return o;
	}

	/**
	 * 反射设置属性值
	 * 
	 * @author M.F.Yang
	 * @param classPath
	 * @param attributeName
	 * @param attributeValue
	 */

	public static void setAttributeValue(String classPath,
			String attributeName, String attributeValue) {
		try {
			Clazz = Class.forName(classPath);
			obj = Clazz.newInstance();

			Field field = Clazz.getDeclaredField(attributeName);
			field.setAccessible(true);
			field.set(obj, attributeValue);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
